package com.future.message.Controller;

import com.future.message.Mapper.RelevantReportMapper;
import com.future.message.Service.UserService;
import com.future.message.Utils.ResponseResult;
import com.future.message.configuration.Security.WebSecurityConfig;
import com.future.message.pojo.SecurityUser;
import com.future.message.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/user")
@Api(tags = "用户")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "username",paramType = "query",value = "账号"),
            @ApiImplicitParam(name = "password",paramType = "query",value = "密码")
    })
    public void login(String username,String password){

    }


    @ApiOperation("管理员添加用户")
    @PostMapping("/addUser")
    @Secured("ROLE_1")
    public ResponseResult addUser(User user){
        if (user == null){
            return ResponseResult.error("用户信息为空！");
        }
        return userService.addUser(user);
    }

    @ApiOperation("管理员删除用户")
    @ApiImplicitParam(name = "userId",value = "用户Id")
    @DeleteMapping("/delete")
    @Secured("ROLE_1")
    public ResponseResult deleteUser(Integer userId){
        if (userId == null){
            return ResponseResult.error("用户Id为空！");
        }
        return userService.deleteUser(userId);
    }

    @ApiOperation("管理员修改用户信息")
    @PutMapping("/update")
    @Secured({"ROLE_1"})
    public ResponseResult updateUser(User user){
        if (user == null ){
            return ResponseResult.error("用户信息为空！");
        }
        return userService.updateUser(user);
    }

    @ApiOperation("管理员分页查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNow",value = "当前页"),
            @ApiImplicitParam(name = "pageSize",value = "每页的数据")
    })
    @Secured("ROLE_1")
    @GetMapping("/queryUserByPage")
    public ResponseResult queryUserByPage(Integer pageNow,Integer pageSize){
        if (pageNow == null || pageNow <= 0){
            return ResponseResult.error("当前页码数不合法！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("每页条数不合法!");
        }
        return userService.queryUserByPage(pageNow,pageSize);
    }

    @ApiOperation("模糊查询")
    @ApiImplicitParam(name = "message",value = "账号，或者昵称")
    @GetMapping("/vagueSearch")
    @Secured("ROLE_1")
    public ResponseResult vagueSearch(String message,Integer pageNow,Integer pageSize){
        if (message == null || message.length() == 0){
            return ResponseResult.error("填写信息为空，请重新输入！");
        }
        if (pageNow == null || pageNow <= 0){
            return ResponseResult.error("当前页码数不合法！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("每页条数不合法!");
        }
        return userService.vagueSearch(message,pageNow,pageSize);
    }

    @ApiOperation("根据Id查找用户信息")
    @GetMapping("/searchByUserId")
    public ResponseResult searchByUserId(Integer userId){
        if (userId == null){
            return ResponseResult.error("参数有误！");
        }
        return userService.searchByUserId(userId);
    }

    @ApiOperation("修改个人信息")
    @PutMapping("/modifyDetail")
    @Secured({"ROLE_1","ROLE_0"})
    public ResponseResult modifyDetail(@AuthenticationPrincipal @ApiIgnore SecurityUser securityUser, String nickedName){
        if (nickedName == null){
            return ResponseResult.error("昵称不能为空！");
        }
        return userService.modifyDetail(securityUser.getCurrentUserInfo().getUserId(),nickedName);
    }

    @ApiOperation("修改密码")
    @PutMapping("/modifyPassword")
    @Secured({"ROLE_1","ROLE_0"})
    public ResponseResult modifyPassword(@AuthenticationPrincipal @ApiIgnore SecurityUser securityUser, String oldPassword, String newPassword){
        if (oldPassword == null || oldPassword.length() < 6 || oldPassword.length() > 16 ){
            return  ResponseResult.error("旧密码不合法！");
        }
        if (newPassword == null || newPassword.length() < 6 || newPassword.length() > 16){
            return ResponseResult.error("新密码不合法！");
        }
        if (oldPassword.equals(newPassword)){
            return ResponseResult.error("新旧密码相同！");
        }
        return userService.modifyPassword(securityUser.getCurrentUserInfo().getUserId(),oldPassword,newPassword);
    }

    @ApiOperation("查询所有用户")
    @PutMapping("/queryForAllUser")
    @Secured({"ROLE_1"})
    public ResponseResult queryForAllUser(){
        return userService.queryForAllUser();
    }
}
